package com.fr.report.web.ui;

import com.fr.base.StringUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.web.Repository;

public abstract class FieldEditor extends Widget
{
  public static final String BEFOREEDIT = "beforeedit";
  public static final String AFTEREDIT = "afteredit";
  public static final String STOPEDIT = "stopedit";
  protected String fieldName;
  protected boolean allowBlank = true;
  protected String errorMessage = "";

  public String getFieldName()
  {
    return this.widgetName;
  }

  public void setFieldName(String paramString)
  {
    this.widgetName = paramString;
  }

  public boolean isAllowBlank()
  {
    return this.allowBlank;
  }

  public void setAllowBlank(boolean paramBoolean)
  {
    this.allowBlank = paramBoolean;
  }

  public String getErrorMessage()
  {
    return ((this.errorMessage == null) ? "" : this.errorMessage);
  }

  public void setErrorMessage(String paramString)
  {
    this.errorMessage = paramString;
  }

  public boolean isEditor()
  {
    return true;
  }

  public String[] dependence()
  {
    return new String[0];
  }

  public String[] supportedEvents()
  {
    return new String[] { "beforeedit", "afteredit", "stopedit" };
  }

  public JSONObject createJSONConfig(Object paramObject, Repository paramRepository)
    throws JSONException
  {
    JSONObject localJSONObject = super.createJSONConfig(paramObject, paramRepository);
    if (StringUtils.isNotBlank(this.fieldName))
      localJSONObject.put("fieldName", this.fieldName);
    if (StringUtils.isNotBlank(this.errorMessage))
      localJSONObject.put("errorMsg", this.errorMessage);
    if (!(this.allowBlank))
      localJSONObject.put("allowBlank", this.allowBlank);
    return localJSONObject;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
    {
      if (paramXMLableReader.getTagName().equals("FieldName"))
        setWidgetName(paramXMLableReader.getElementValue());
      if (paramXMLableReader.getTagName().equals("EMSG"))
        this.errorMessage = paramXMLableReader.getElementValue();
      if (paramXMLableReader.getTagName().equals("allowBlank"))
        this.allowBlank = false;
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    super.writeXML(paramXMLPrintWriter);
    if (StringUtils.isNotBlank(this.errorMessage))
      paramXMLPrintWriter.startTAG("EMSG").textNode(this.errorMessage).end();
    if (!(this.allowBlank))
      paramXMLPrintWriter.startTAG("allowBlank").textNode(String.valueOf(this.allowBlank)).end();
  }

  public boolean equals(Object paramObject)
  {
    return ((paramObject instanceof FieldEditor) && (super.equals(paramObject)) && (ComparatorUtils.equals(this.errorMessage, ((FieldEditor)paramObject).errorMessage)) && (this.allowBlank == ((FieldEditor)paramObject).allowBlank));
  }

  public String toString()
  {
    return "$" + this.fieldName;
  }
}